# -*- coding: utf-8 -*-

import scrapy
from urllib.parse import urljoin
from bs4 import BeautifulSoup
from ..base_parser import BasePageParser

class AboutPageParser(BasePageParser):
    """SFPE新闻列表页面解析器"""
    
    def can_handle(self, response):
        """判断是否能处理该响应"""
        site = [
            'https://www.sfpe.org/about-sfpe/about',
            'https://www.sfpe.org/publications/standards',
        ]
        return response.url in site and response.meta.get('page_type') != 'sfpe_news_detail'
    
    def parse(self, response):
        """解析新闻列表页面"""
        soup = BeautifulSoup(response.text, 'lxml')
        ul_index = 0 if 'about-sfpe' in response.url else 1
        header = soup.select('#MPButtonBar li.dropdown')[ul_index]
        a_list = header.select('ul li a')
        self.logger.info(f"找到{len(a_list)}个链接")
        for a in a_list:
            href = a.get('href')
            if not href:
                continue

            full_url = urljoin(response.url, href)
            if 'publications/fpemagazine/fpeextra' in href:
                # 只需要一个入口
                if href == '/publications/fpemagazine/fpeextra':
                    yield scrapy.Request(
                        url=full_url,
                        callback=self.spider.parse,
                        meta={
                            'page_type': 'sfpe_fpe_extra',
                            'category_url': response.url
                        }
                    )
            elif 'publications/journal/jfpearchives' in href:
                yield scrapy.Request(
                    url=full_url,
                    callback=self.spider.parse,
                    meta={
                        'page_type': 'sfpe_journal',
                        'category_url': response.url
                    }
                )
            elif href == '/publications/periodicals/sfpeeuropedigital':
                yield scrapy.Request(
                    url=full_url,
                    callback=self.spider.parse,
                    meta={
                        'page_type': 'sfpeeuropedigital',
                        'category_url': response.url
                    }
                )

            else:
                yield scrapy.Request(
                    url=full_url,
                    callback=self.spider.parse,
                    meta={
                        'page_type': 'sfpe_news_detail',
                        'category_url': response.url
                    }
                )
        
     